package tasks

import (
	"log"
	"shortUrl/services/mysql"
	"time"

	"github.com/robfig/cron/v3"
)

func StartCleanupTask(urlService mysql.ServiceUrler, cronSpec string) {
	c := cron.New()

	_, err := c.AddFunc(cronSpec, func() {
		log.Println("Starting cleanup of expired URLs...")
		startTime := time.Now()
		
		if err := urlService.DelateUrl(); err != nil {
			log.Printf("Cleanup failed: %v\n", err)
		} else {
			log.Printf("Cleanup completed in %v\n", time.Since(startTime))
		}
	})
	
	if err != nil {
		log.Fatalf("Failed to schedule cleanup task: %v", err)
	}

	c.Start()
	log.Printf("Scheduled cleanup task with spec: %s\n", cronSpec)
}